package com.macower.businessdata.action;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.macower.businessdata.biz.OrderBiz;
import com.macower.businessdata.biz.OrderDetailBiz;
import com.macower.businessdata.dto.OrderDto;
import com.macower.businessdata.entity.Member;
import com.macower.businessdata.entity.Order;
import com.macower.businessdata.entity.OrderDetail;
import com.macower.businessdata.util.EncodeUtil;
import com.macower.core.entity.Page;
import com.macower.core.exception.BizException;
import com.macower.core.util.JsonUtils;
import com.macower.core.util.StringUtils;
import com.macower.sys.biz.ConfigBiz;
import com.macower.sys.entity.Config;

@Controller
@RequestMapping(value = "/order")
public class OrderAction {

	@Autowired
	private OrderBiz orderBiz;
	
	@Autowired
	private ConfigBiz configBiz;
	
	@Autowired
	private OrderDetailBiz orderDetailBiz;
	

	/**
	 * 后台管理-订单管理页面
	 * @return
	 */
	@RequestMapping({ "/main" })
	public String main() {
		return "businessdata/order";
		
	}
	/**
	 * 后台管理-待付款订单
	 * @return
	 */
	@RequestMapping({ "/awaitingPayOrder" })
	public String awaitingPayOrder() {
		return "businessdata/awaitingPayOrder";
		
	}
	
	/**
	 * 后台管理-待发货订单
	 * @return
	 */
	@RequestMapping({ "/prepareSendOrder" })
	public String prepareSendOrder() {
		return "businessdata/prepareSendOrder";
		
	}
	/**
	 * 后台管理-已发货订单
	 * @return
	 */
	@RequestMapping({ "/sendOrder" })
	public String sendOrder() {
		return "businessdata/sendOrder";
		
	}
	/**
	 * 后台管理-已完成订单
	 * @return
	 */
	@RequestMapping({ "/completedOrder" })
	public String completedOrder() {
		return "businessdata/completedOrder";
		
	}
	/**
	 * 后台管理-已取消订单
	 * @return
	 */
	@RequestMapping({ "/cancelledOrder" })
	public String cancelledOrder() {
		return "businessdata/cancelledOrder";
		
	}
	/**
	 * 后台管理-拒绝退款订单
	 * @return
	 */
	@RequestMapping({ "/declinedOrder" })
	public String declinedOrder() {
		return "businessdata/declinedOrder";
		
	}
	/**
	 * 后台管理-申请退款订单
	 * @return
	 */
	@RequestMapping({ "/refundApplyOrder" })
	public String refundApplyOrder() {
		return "businessdata/refundApplyOrder";
		
	}
	/**
	 * 后台管理-申请退款审核通过订单
	 * @return
	 */
	@RequestMapping({ "/auditedOrder" })
	public String auditedOrder() {
		return "businessdata/auditedOrder";
		
	}
	
	
	/**
	 * 后台管理-已退款订单
	 * @return
	 */
	@RequestMapping({ "/refundedOrder" })
	public String refundedOrder() {
		return "businessdata/refundedOrder";
		
	}
	
	/**
	 * 后台管理 - 分页查询
	 * @param obj
	 * @param response
	 */
	@RequestMapping({ "/list" })
	public void list(OrderDto obj, HttpServletResponse response) {
		List<Order> root = new ArrayList<Order>();
		Page<Order> page = orderBiz.findPageBy(obj,
				obj.getStart() / obj.getLimit(), obj.getLimit());
		root = ((List<Order>) page.getData());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);

		map.put("root", root);
		map.put("totalSize", page.getTotalSize());

		JsonUtils.returnJson(response, map);
	}
	/**
	 * 后台管理-删除记录
	 * 
	 * @param ids
	 * @param response
	 */
	@RequestMapping({ "/delete" })
	public void delete(String id, HttpServletRequest request,
			HttpServletResponse response) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.deletes(id);
		} catch (Exception e) {
			// 删除捕获所有的异常
			map.put("success", false);
			map.put("msg", e.getMessage());
			e.printStackTrace();
		}

		JsonUtils.returnJson(response, map);
	}
	
	
	/**
	 * 后台管理-打折
	 * 
	 * @param obj
	 * @param response
	 */
	@RequestMapping({ "/discount" })
	public void discount(HttpServletRequest request ,HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);

		try {
			String orderId = request.getParameter("id") ;//订单id
			String discountOrderTotalPrice = request.getParameter("discountOrderTotalPrice") ; //折扣订单总金额
			String discountPrice = request.getParameter("discountPrice") ;//折扣金额
			String orderDetailList = request.getParameter("orderDetailList") ; //订单详情list
			
			this.orderBiz.discount(orderId,discountOrderTotalPrice,discountPrice,orderDetailList) ;
			
		} catch (BizException e) {
			e.printStackTrace();
			map.put("msg", e.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
			map.put("error", e1.getMessage());
			map.put("success", false);
		}

		JsonUtils.returnJson(response, map);
	}
	
	/**
	 * 前台-我的订单
	 */
	@RequestMapping({ "/toMyOrder" })
	public String toMyOrder(Model model ,HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		String pageIndex_Str = (String) request.getParameter("pageIndex");
		String month = null ;
		Integer pageIndex = 1;
		if (pageIndex_Str == null) {//默认查询第一页
			pageIndex = 1;
			month = request.getParameter("toMyOrderParam_month");
			session.setAttribute("toMyOrderParam_month", (month==null)?"1":month);
		} else{
			pageIndex_Str = (String) request.getParameter("pageIndex");
			pageIndex = Integer.parseInt(pageIndex_Str);
			month = (String) session.getAttribute("toMyOrderParam_month");
		}
		Integer pageSize = 10 ;
		Config c = this.configBiz.findFromCacheByConfigCode("orderPageSize") ;
		try {
			pageSize = (c == null)?10:Integer.parseInt(c.getConfigValue()) ;
		} catch (NumberFormatException e) {
			pageSize = 10 ;
		}
		Member member = (Member)request.getSession().getAttribute("member") ;
		OrderDto param = new OrderDto() ;
		if(member != null){
			param.setMemberId(member.getId()) ;
		}else{
			param.setSessionId(request.getSession().getId()) ;
		}
		param.setMonth((month==null)?"1":month) ;
		Page<Order> page = this.orderBiz.findMyOrder(param, pageIndex - 1, pageSize);
		
		model.addAttribute("page", page) ;
		
		return "product/myOrder";
		
	}
	
	/**
	 * 前台-取消订单
	 */
	@RequestMapping({ "/{id}/cancelOrder" })
	public String cancelOrder(@PathVariable Long id ) {
		
		// 更改订单状态
		this.orderBiz.cancelOrder(id) ;
		
		return "redirect:/order/toMyOrder";
		
	}
	
	/**
	 * 前台-申请退款
	 */
	@RequestMapping({ "/redundApply" })
	public String redundApply(Order obj) {
		
		// 更改订单状态
		Order old = this.orderBiz.get(obj.getId()) ;
		if(old.getOrderStatus() == 2){ //已付款才可以申请退款
			old.setOrderStatus(6) ; //申请退款
			old.setRefundApplyTm(new Date()) ;
			old.setRefundApplyDesc(obj.getRefundApplyDesc()) ;
			old.setRefundApplyReason(obj.getRefundApplyReason()) ;
			this.orderBiz.update(old) ;
		}
		
		return "redirect:/order/toMyOrder";
		
	}
	/**
	 * 前台-查询物流
	 */
	@RequestMapping({ "/trackShipment" })
	public String trackShipment(Order obj) {
		
		return "product/trackShipment";
		
	}
	/**
	 * 前台-查询物流
	 */
	@RequestMapping({ "/findShippingNoByOrderNo" })
	public void findShippingNoByOrderNo(String orderNo,HttpServletResponse response) {
		String value = null ;
		Order order = this.orderBiz.findShippingNoByOrderNo(orderNo) ;
		Map<String, Object> map = new HashMap<String, Object>();
		if(order != null){
			if(StringUtils.isEmpty(order.getShippingNo())){
				value = "empty" ;
			}else{
				value = order.getShippingNo() ;
			}
			
			map.put("success", value);
		}else{
			value = "no" ;
			map.put("success", value);
		}
		
		JsonUtils.returnJson(response,map) ;
		
	}
	
	
	
	
	
	/*
	 * 前台-生成订单
	 * 判断是否登录，未登录存储sessionid  已登录存储memberid
	 */
	@RequestMapping({ "/gernarateOrder" })
	public String gernarateOrder(HttpServletRequest request) {
		String returlUrl = "redirect:/order/toMyOrder";

		//生成订单 生成订单详情数据  清空购物车
		String[]  item_productIds = request.getParameterValues("item_productId");
		if(item_productIds != null){
			this.orderBiz.createOrder(request);
		}else{
			//跳回我的购物车
			returlUrl = "redirect:/product/toMyCart";
		}
		//跳转到我的订单
		return returlUrl;
		
	}

	/*
	 * 后台：发货
	 */
	@RequestMapping({ "/dispatch" })
	public void dispatch(Order obj, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.dispatch(obj);
		} catch (BizException e) {
			e.printStackTrace();
			map.put("msg", e.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
			map.put("error", e1.getMessage());
			map.put("success", false);
		}

		JsonUtils.returnJson(response, map);
	}
	
	
	/*
	 * 后台：修改发货物流信息  在已完成的订单里可以修改发货物流信息
	 */
	@RequestMapping({ "/updateShppingInfo" })
	public void updateShppingInfo(Order obj, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.updateShppingInfo(obj);
		} catch (BizException e) {
			e.printStackTrace();
			map.put("msg", e.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
			map.put("error", e1.getMessage());
			map.put("success", false);
		}

		JsonUtils.returnJson(response, map);
	}
	
	/*
	 * 后台：退款 ajax
	 */
	@RequestMapping({ "/refunded" })
	public void refunded(Order obj, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.refunded(obj);
		}catch (Exception e) {
			//删除捕获所有的异常
			map.put("success", false);
			map.put("msg", e.getMessage());
			e.printStackTrace();
		}

		JsonUtils.returnJson(response, map);
	}
	
	
	/*
	 * 后台：发货邮件通知
	 */
	@RequestMapping({ "/dispatchSendMail" })
	public void dispatchSendMail(String ids, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.dispatchSendMail(ids);
		}catch (Exception e) {
			map.put("success", false);
			map.put("msg", e.getMessage());
			e.printStackTrace();
		}

		JsonUtils.returnJson(response, map);
	}
	/*
	 * 后台：关闭交易 ajax
	 */
	@RequestMapping({ "/completed" })
	public void completed(Order obj, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.completed(obj);
		}catch (Exception e) {
			//删除捕获所有的异常
			map.put("success", false);
			map.put("msg", e.getMessage());
			e.printStackTrace();
		}

		JsonUtils.returnJson(response, map);
	}
	
	/*
	 * 后台：同步邮箱到目标客户 ajax
	 */
	@RequestMapping({ "/sychToTaskEmail" })
	public void sychToTaskEmail(String ids, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.sychToTaskEmail(ids);
		}catch (Exception e) {
			//AJAX捕获所有的异常
			map.put("success", false);
			map.put("msg", e.getMessage());
			e.printStackTrace();
		}

		JsonUtils.returnJson(response, map);
	}
	
	/*
	 * 后台：拒绝退款
	 */
	@RequestMapping({ "/declined" })
	public void delined(Order obj, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.declined(obj);
		} catch (BizException e) {
			e.printStackTrace();
			map.put("msg", e.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
			map.put("error", e1.getMessage());
			map.put("success", false);
		}

		JsonUtils.returnJson(response, map);
	}
	
	/*
	 * 后台：退款申请-》审核通过
	 */
	@RequestMapping({ "/audit" })
	public void audit(Order obj, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		try {
			orderBiz.audit(obj);
		} catch (BizException e) {
			e.printStackTrace();
			map.put("msg", e.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
			map.put("error", e1.getMessage());
			map.put("success", false);
		}

		JsonUtils.returnJson(response, map);
	}
	/*
	 * 后台：导出待发货订单
	 */
	@RequestMapping({ "/exportPreSendOrder" })
	public void exportPreSendOrder(OrderDto obj, HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("UTF-8");
			obj.setOrderStatus(2) ; //查询已付款的
			List<Order> data = orderBiz.findBy(obj);
			InputStream template = this.getClass().getResourceAsStream(
					"/com/macower/businessdata/tpl/preSendOrderTpl.xls");
			HSSFWorkbook workbook = new HSSFWorkbook(template);
			HSSFSheet sheet = workbook.getSheetAt(0);
			writeDataToSheet(sheet, data);

			String filename = "待发货订单.xls";
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename="
					+ EncodeUtil.encodeFilename(filename, request));
			OutputStream ouputStream = response.getOutputStream();
			workbook.write(ouputStream);
			ouputStream.flush();
			ouputStream.close();

		} catch (Exception e) {
			// 捕获所有的异常
			e.printStackTrace();
		}
	}
	
	
	private void writeDataToSheet(HSSFSheet sheet, List<Order> data) {
		HSSFCellStyle headStyle = null ;
		HSSFCellStyle valueStyle = null ;
		HSSFRow headRowModel = sheet.getRow(0);
		HSSFRow valueRowModel = sheet.getRow(1);
		headStyle = headRowModel.getCell(0).getCellStyle() ;
		valueStyle = valueRowModel.getCell(0).getCellStyle() ;
		
		HSSFRow rowModel1 = sheet.getRow(0);
		HSSFRow rowModel2 = sheet.getRow(1);
		HSSFRow rowModel3 = sheet.getRow(2);
		HSSFRow rowModel4 = sheet.getRow(3);
		HSSFRow rowModel5 = sheet.getRow(4);
		HSSFRow rowModel6 = sheet.getRow(5);
		
		sheet.removeRow(rowModel1);
		sheet.removeRow(rowModel2);
		sheet.removeRow(rowModel3);
		sheet.removeRow(rowModel4);
		sheet.removeRow(rowModel5);
		sheet.removeRow(rowModel6);

		int rownum = 1; // 从第1行开始写
		rownum = rownum - 1;
		HSSFCell cell = null ;
		for (Order obj : data) {
			//第一行
			HSSFRow row1 = sheet.createRow(rownum++);
			cell = row1.createCell(0);//订单号
			cell.setCellValue(rowModel1.getCell(0).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row1.createCell(1);//订单总金额
			cell.setCellValue(rowModel1.getCell(1).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row1.createCell(2);//物流
			cell.setCellValue(rowModel1.getCell(2).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row1.createCell(3);//物流费用
			cell.setCellValue(rowModel1.getCell(3).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			//第二行
			HSSFRow row2 = sheet.createRow(rownum++);
			cell = row2.createCell(0);
			cell.setCellValue(obj.getOrderNo());//订单号
			cell.setCellStyle(valueStyle) ;
			
			cell = row2.createCell(1);
			cell.setCellValue(obj.getGrossAmount()+"");//订单总金额
			cell.setCellStyle(valueStyle) ;
			
			cell = row2.createCell(2);
			cell.setCellValue(obj.getShippingOptionName());//物流
			cell.setCellStyle(valueStyle) ;
			
			cell = row2.createCell(3);
			cell.setCellValue(obj.getShippingOptionAmount()+"");//物流费用
			cell.setCellStyle(valueStyle) ;
			//第三行
			HSSFRow row3 = sheet.createRow(rownum++);
			cell = row3.createCell(0);
			cell.setCellValue(rowModel3.getCell(0).getStringCellValue());//详细地址
			cell.setCellStyle(headStyle) ;
			
			cell = row3.createCell(1);
			cell.setCellValue(rowModel3.getCell(1).getStringCellValue());//收件人
			cell.setCellStyle(headStyle) ;
			
			cell = row3.createCell(2);
			cell.setCellValue(rowModel3.getCell(2).getStringCellValue());//联系电话
			cell.setCellStyle(headStyle) ;
			
			cell = row3.createCell(3);
			cell.setCellValue(rowModel3.getCell(3).getStringCellValue());//email
			cell.setCellStyle(headStyle) ;
			//第四行
			HSSFRow row4 = sheet.createRow(rownum++);
			cell = row4.createCell(0);
			String street2 = obj.getStreet2() ;
			if(StringUtils.isEmpty(street2)){
				street2 = "" ;
			}else{
				street2 = ","+street2+" " ;
			}
			
			cell.setCellValue(obj.getStreet1()+" "+street2+obj.getCity()+","+obj.getState()+" "+obj.getZipCode()+" "+obj.getCountryName()); //详细地址
			cell.setCellStyle(valueStyle) ;
			
			cell = row4.createCell(1);
			cell.setCellValue(obj.getFirstName()+" "+obj.getLastName());//收件人
			cell.setCellStyle(valueStyle) ;
			
			cell = row4.createCell(2);
			cell.setCellValue(obj.getPhoneNum());//联系电话
			cell.setCellStyle(valueStyle) ;
			
			cell = row4.createCell(3);
			cell.setCellValue(obj.getEmail());//email
			cell.setCellStyle(valueStyle) ;
			//第五行
			HSSFRow row5 = sheet.createRow(rownum++);
			cell = row5.createCell(0);
			cell.setCellValue(rowModel5.getCell(0).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row5.createCell(1);
			cell.setCellValue(rowModel5.getCell(1).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row5.createCell(2);
			cell.setCellValue(rowModel5.getCell(2).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row5.createCell(3);
			cell.setCellValue(rowModel5.getCell(3).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row5.createCell(4);
			cell.setCellValue(rowModel5.getCell(4).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			
			cell = row5.createCell(5);
			cell.setCellValue(rowModel5.getCell(5).getStringCellValue());
			cell.setCellStyle(headStyle) ;
			OrderDetail param = new OrderDetail() ;
			param.setOrderId(obj.getId()) ;
			List<OrderDetail> details = this.orderDetailBiz.findBy(param) ;
			for(OrderDetail detail : details){
				//第六行
				HSSFRow row6 = sheet.createRow(rownum++);
				cell = row6.createCell(0);
				cell.setCellValue(detail.getProductName());
				cell.setCellStyle(valueStyle) ;
				
				cell = row6.createCell(1);
				cell.setCellValue(detail.getBasePrice()+"");
				cell.setCellStyle(valueStyle) ;
				
				cell = row6.createCell(2);
				cell.setCellValue(detail.getUnitPrice()+"");
				cell.setCellStyle(valueStyle) ;
				
				cell = row6.createCell(3);
				cell.setCellValue(detail.getColor());
				cell.setCellStyle(valueStyle) ;
				
				cell = row6.createCell(4);
				cell.setCellValue(detail.getSize());
				cell.setCellStyle(valueStyle) ;
				
				cell = row6.createCell(5);
				cell.setCellValue(detail.getAmount());
				cell.setCellStyle(valueStyle) ;
			}
			sheet.createRow(rownum++);
			
		}
		
	}
	/*
	 * 后台：导出已发货订单
	 */
	@RequestMapping({ "/exportSendOrder" })
	public void exportSendOrder(OrderDto obj, HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("UTF-8");
			obj.setOrderStatus(3) ; //查询已发货的
			List<Order> data = orderBiz.findBy(obj);
			InputStream template = this.getClass().getResourceAsStream(
					"/com/macower/businessdata/tpl/sendOrderTpl.xls");
			HSSFWorkbook workbook = new HSSFWorkbook(template);
			HSSFSheet sheet = workbook.getSheetAt(0);
			writeDataToSheet4SendOrder(sheet, data);

			String filename = "已发货订单.xls";
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename="
					+ EncodeUtil.encodeFilename(filename, request));
			OutputStream ouputStream = response.getOutputStream();
			workbook.write(ouputStream);
			ouputStream.flush();
			ouputStream.close();

		} catch (Exception e) {
			// 捕获所有的异常
			e.printStackTrace();
		}
	}
	@SuppressWarnings("deprecation")
	private void writeDataToSheet4SendOrder(HSSFSheet sheet, List<Order> data) {
		List<HSSFCellStyle> style = new ArrayList<HSSFCellStyle>(0);
		HSSFRow rowModel = sheet.getRow(1);
		for (int i = 0; i < 8; i++) {
			style.add(rowModel.getCell(i).getCellStyle());
		}
		sheet.removeRow(rowModel);

		int rownum = 2; // 从第二行开始写
		rownum = rownum - 1;
		for (Order obj : data) {
			HSSFRow row = sheet.createRow(rownum++);
			short column = 0;
			//订单号
			HSSFCell cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			cell0.setCellValue(new HSSFRichTextString(obj.getOrderNo()));
			//订单总金额
			cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			cell0.setCellValue(new HSSFRichTextString(obj.getGrossAmount()+""));
			
			//发货物流号
			cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			cell0.setCellValue(new HSSFRichTextString(obj.getShippingNo()));
			
			//发货物流
			cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			cell0.setCellValue(new HSSFRichTextString(obj.getShippingOptionName()+""));
			
			//收件人姓名
			cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			cell0.setCellValue(new HSSFRichTextString(obj.getFirstName()+" "+obj.getLastName()));
			
			//收件人邮箱
			cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			cell0.setCellValue(new HSSFRichTextString(obj.getEmail()));
			
			//paypal交易号
			cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			cell0.setCellValue(new HSSFRichTextString(obj.getTransactionId()));
			
			//收货地址
			cell0 = row.createCell(column++);
			cell0.setCellStyle(style.get(column - 1));
			String street2 = obj.getStreet2() ;
			if(StringUtils.isEmpty(street2)){
				street2 = "" ;
			}else{
				street2 = ","+street2+" " ;
			}
			
			cell0.setCellValue(obj.getStreet1()+" "+street2+obj.getCity()+","+obj.getState()+" "+obj.getZipCode()+" "+obj.getCountryName()); //详细地址
			
			
		}

		
	}
	
	/*
	 * 后台：导出已完成订单
	 */
	@RequestMapping({ "/exportCompleteOrder" })
	public void exportCompleteOrder(OrderDto obj, HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("UTF-8");
			obj.setOrderStatus(4) ; //查询已完成
			List<Order> data = orderBiz.findBy(obj);
			InputStream template = this.getClass().getResourceAsStream(
					"/com/macower/businessdata/tpl/sendOrderTpl.xls");
			HSSFWorkbook workbook = new HSSFWorkbook(template);
			HSSFSheet sheet = workbook.getSheetAt(0);
			writeDataToSheet4SendOrder(sheet, data);

			String filename = "已完成订单.xls";
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename="
					+ EncodeUtil.encodeFilename(filename, request));
			OutputStream ouputStream = response.getOutputStream();
			workbook.write(ouputStream);
			ouputStream.flush();
			ouputStream.close();

		} catch (Exception e) {
			// 捕获所有的异常
			e.printStackTrace();
		}
	}

}
